package a_aa_amainpackage.qiye_test.address;

import java.sql.*;
import java.util.Properties;

/**
 * @ Auther: o2o-rd-0008
 * @ Date:   2020/12/23 16:21
 * @ Param:  ${PARAM}
 * @ Description:
 */
public class Test {


    public static void main(String[] args) {
        Connection connection = GetConnectionzf("sysadmin", "Bigdata@123");
        Connection connectionholo = GetConnectionHolo("LTAI4GEATD3S6mXkz3pEoELY", "053TJDDYTrUmSZlrBWt3rocmfKRLSa");

        try {
            Statement statement = connection.createStatement();
            Statement statementholo = connectionholo.createStatement();


            ResultSet resultSet = statement.executeQuery(
                    "select \"boss_name\",\"licence_num\",address,longitude,latitude" +
                            ",\"registration_institution\",\"credit_code\",\"company_name\"" +
                            ",\"region_id\",email,phone,\"approval_date\",\"business_scope\"" +
                            ",\"business_status\",\"enterprise_type\",industry,\"organization_code\"" +
                            ",\"registered_capital\",\"registration_time\",\"taxpayer_identification_number\"" +
                            ",'2017-01-01 00:00:00','9999-12-31 00:00:00' from \"test_zsc\".\"o2o_stand_company_IDL\"");

            while (resultSet.next()){
                StringBuilder sb = new StringBuilder();
                sb.append("insert into dimension.dim_standard_enterprice (legal_person,business_registration_number,address,longitude,latitude" +
                        ",registration_institution,credit_code,company_name,region_id,email,phone,approval_date,business_scope,business_status" +
                        ",enterprise_type,industry,organization_code,registered_capital,registration_time,taxpayer_identification_number,startDate,endDate)\n");
                sb.append("values\n");
                sb.append("(\n");

                sb.append("E'"+resultSet.getString(1)+"',");
                sb.append("E'"+resultSet.getString(2)+"',");
                sb.append("E'"+resultSet.getString(3)+"',");
                sb.append("E'"+resultSet.getString(4)+"',");
                sb.append("E'"+resultSet.getString(5)+"',");
                sb.append("E'"+resultSet.getString(6)+"',");
                sb.append("E'"+resultSet.getString(7)+"',");
                sb.append("E'"+resultSet.getString(8)+"',");
                sb.append("E'"+resultSet.getString(9)+"',");
                sb.append("E'"+resultSet.getString(10)+"',");
                sb.append("E'"+resultSet.getString(11)+"',");
                sb.append("E'"+resultSet.getString(12)+"',");
                sb.append("E'"+resultSet.getString(13)+"',");
                sb.append("E'"+resultSet.getString(14)+"',");
                sb.append("E'"+resultSet.getString(15)+"',");
                sb.append("E'"+resultSet.getString(16)+"',");
                sb.append("E'"+resultSet.getString(17)+"',");
                sb.append("E'"+resultSet.getString(18)+"',");
                sb.append("E'"+resultSet.getString(19)+"',");
                sb.append("E'"+resultSet.getString(20)+"',");
                sb.append("E'"+resultSet.getString(21)+"',");
                sb.append("E'"+resultSet.getString(22)+"'\n");

                sb.append(")\n");
                try {
                    statementholo.execute(sb.toString());
                }catch (SQLException e){
                    System.out.println(sb.toString());
                }

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public static Connection GetConnectionzf(String username, String passwd) {
        //定义驱动类。
        String driver = "org.postgresql.Driver";
//192.168.2.234
        //192.168.2.148
        Properties props = new Properties();
        props.setProperty("user", username);
        props.setProperty("password", passwd);
        String url = "jdbc:postgresql://" + "192.168.2.234" + ':'
                + "8000" + '/'
                + "postgres";
        Connection conn = null;
        try {
            //加载驱动。
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            //创建连接。
            conn = DriverManager.getConnection(url, props);
            System.out.println("Connection succeed!");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return conn;
    }

    public static Connection GetConnectionHolo(String username, String passwd) {
        //定义驱动类。
        String driver = "org.postgresql.Driver";
//192.168.2.234
        //192.168.2.148
        Properties props = new Properties();
        props.setProperty("user", username);
        props.setProperty("password", passwd);
        String url = "jdbc:postgresql://" + "hgprecn-cn-m7r1xxicx002-cn-beijing.hologres.aliyuncs.com" + ':'
                + "80" + '/'
                + "testdb";
        Connection conn = null;
        try {
            //加载驱动。
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            //创建连接。
            conn = DriverManager.getConnection(url, props);
            System.out.println("Connection succeed!");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return conn;
    }
}
